import numpy as np
import csv
import random
from datetime import datetime
from time import strftime
import itertools as itertools
import math as math
import bisect

'''
Global and Local Empirical Bayes Smoothers with Gamma Model
'''

def getCurTime():
    """
    get current time
    Return value of the date string format(%Y-%m-%d %H:%M:%S)
    """
    format='%Y-%m-%d %H:%M:%S'
    sdate = None
    cdate = datetime.now()
    try:
        sdate = cdate.strftime(format)
    except:
        raise ValueError
    return sdate

def build_data_list(inputCSV):
    sKey = []
    fn = inputCSV
    ra = csv.DictReader(file(fn), dialect="excel")
    
    for record in ra:
        #print record[ra.fieldnames[0]], type(record[ra.fieldnames[-1]])
        for item in ra.fieldnames:
            temp = float(record[item])
            sKey.append(temp)
    sKey = np.array(sKey)
    sKey.shape=(-1,len(ra.fieldnames))
    return sKey

class WeightedRandomGenerator(object):
    def __init__(self, weights):
        self.totals = []
        running_total = 0

        for w in weights:
            running_total += w
            self.totals.append(running_total)

    def next(self):
        rnd = random.random() * self.totals[-1]
        return bisect.bisect_right(self.totals, rnd)

    def __call__(self):
        return self.next()

#--------------------------------------------------------------------------
#MAIN

if __name__ == "__main__":
    print "begin at " + getCurTime()

    filepath = 'C:/_DATA/migration89_08/COUNTY Migration/clean/distinct_fips.csv'
    countyInfo = build_data_list(filepath)

    filepath = 'C:/_DATA/migration89_08/COUNTY Migration/clean/distinct_state_id.csv'
    stateInfo = build_data_list(filepath)

    fips_dic = {}
    for item in countyInfo:
        fips_dic[str(int(item[0]))] = int(item[1])
    #print fips_dic

    state_dic = {}
    for item in stateInfo:
        state_dic[str(int(item[0]))] = int(item[4])
    #print state_dic

    year = '2009'

    flowCSV = 'C:/_DATA/migration89_08/COUNTY Migration/clean/' + year +'_in_within_state_flow.csv'
    flowData = build_data_list(flowCSV)
    fixCountyList = {}
    
    for item in range(0, len(fips_dic)):
        fixCountyList[str(int(item))] = [item]
    #print fixCountyList
        
    for item in flowData:
        oid = fips_dic[str(int(item[2]))]
        did = fips_dic[str(int(item[5]))]
        #print oid, did
        fixCountyList[str(oid)].append(did)

    #weightedRandom = WeightedRandomGenerator(temp_cancer_weight)
    inCSV = 'C:/_DATA/migration89_08/COUNTY Migration/clean/' + year +'_in_within_state_count.csv'
    inData = build_data_list(inCSV)
    
    outCSV = 'C:/_DATA/migration89_08/COUNTY Migration/clean/' + year +'_out_within_state_count.csv'
    outData = build_data_list(outCSV)
    
    output = []

    id = 0
    for item in inData:
        print id, item[0]
    #if 1>0:
        #item = inData[id,:]
        totalmiss = int(item[3])
        stateID = state_dic[str(int(item[0]/1000))]
        weight = []
        for j in outData[int(stateInfo[stateID,1]):int(stateInfo[stateID,2])+1,-1]:
            weight.append(j)
        for p in fixCountyList[str(id)]:
            weight[int(p-stateInfo[stateID,1])] = 0
        weightedRandom = WeightedRandomGenerator(weight)
        temp = []
        for i in range(0, len(weight)):
            temp.append([int(item[0]),int(countyInfo[int(i+stateInfo[stateID,1]), 0]), 0])
        #if id == 1776:
            #print weight
            
        i = 0
        while (i < totalmiss):
            temp_id = weightedRandom.next()
            if temp_id in fixCountyList[str(id)]:
                continue
            #elif temp[temp_id][-1] > 9:
                #continue
            else:
                temp[temp_id][-1] += 1
                i += 1
        for t in temp:
            if t[-1] > 0:
                output.append(t)
        id += 1
    #print output
    
    np.savetxt(outCSV[:-9] + '_distribute.csv', output, delimiter=',', fmt = '%i')
    